package cn.fl.spl.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.spl.service.ICstRelInfoViewService;
import cn.fl.spl.transform.CstRelInfoViewVOTransform;
import cn.fl.spl.vo.CstRelInfoViewVO;
import com.alibaba.fastjson.JSON;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.spl.facade.framework.constant.DictionaryConstant;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.view.api.ICstRelInfoViewApiService;
import fl.spl.facade.view.condition.CstRelInfoViewQC;
import fl.spl.facade.view.dto.CstRelInfoViewDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description: 供应商拜访记录 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get*
 *               批量：batch* 查询集合：search*
 */
@Service
public class CstRelInfoViewServiceImpl implements ICstRelInfoViewService {

	/**
	 * 客户关系信息 服务层BEAN
	 */
	@Autowired
	private ICstRelInfoViewApiService cstRelInfoViewApiService;
	/**
	 * 数据字典
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	
	@Override
	public CstRelInfoViewVO selectById(Long id) throws BaseException {
		if (StringUtils.isEmpty(id)) {
			throw new BaseException(BaseExceptionConstant.COMMON_0001, "获取对象主键不能为空!");
		}
		// TODO 业务逻辑
		CstRelInfoViewDTO dto = null;// this.自定义ApiService.selectSplVisitRecDTOById(id);
		return CstRelInfoViewVOTransform.toVO(dto);
	}

	@Override
	public Long add(CstRelInfoViewVO entity) throws BaseException {
		if (null == entity) {
			throw new BaseException(BaseExceptionConstant.COMMON_0001, "新增对象不存在!");
		}
		;
		// return
		// this.splVisitRecApiService.add(SplVisitRecVOTransform.toDTO(entity));
		// TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException {
		if (StringUtils.isEmpty(id)) {
			throw new BaseException(BaseExceptionConstant.COMMON_0001, "删除对象主键不能为空!");
		}
		;
		// return this.splVisitRecApiService.removeSplVisitRecDTOById(id);
		// TODO 业务逻辑
		return false;
	}

	/**
	 * 删除
	 * 
	 * @param ids
	 * @return
	 */
	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException {
		if (CollectionUtils.isEmpty(ids)) {
			throw new BaseException(BaseExceptionConstant.COMMON_0001, "删除对象主键不能为空!");
		}
		;
		return false;
	}

	@Override
	public boolean modify(CstRelInfoViewVO entity) throws BaseException {
		if (null == entity) {
			throw new BaseException(BaseExceptionConstant.COMMON_0001, "修改对象不能为空!");
		}
		;
		// return
		// this.splVisitRecApiService.modifySplVisitRecDTO(SplVisitRecVOTransform.toDTO(entity));
		// TODO 业务逻辑
		return false;
	}

	/**
	 * 分页查询所有
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pager searchListPage(Pager page, String sysCd, String orgCd, String chlCd) throws BaseException {
		String methodName = "searchListPage";
		if (StringUtils.isEmpty(sysCd)) {
			BaseLogUtils.newWebLogger("系统代码为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return null;
		}
    	if (StringUtils.isEmpty(orgCd)) {
			BaseLogUtils.newWebLogger("组织架构代码为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return null;
		}
    	if (StringUtils.isEmpty(chlCd)) {
			BaseLogUtils.newWebLogger("渠道代码为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return null;
		}
    	if (null == page) {
			page = new Pager();
			CstRelInfoViewQC cstRelInfoViewQC = new CstRelInfoViewQC();
			page.setCondition(cstRelInfoViewQC);
		}
		Pager pager = this.cstRelInfoViewApiService.selectCstRelInfoViewByPage(page, sysCd, orgCd, chlCd);
		if (null == pager) {
			return null;
		} else {
			List<CstRelInfoViewDTO> dtoList = (List<CstRelInfoViewDTO>) pager.getResultList();
			List<CstRelInfoViewVO> voList = CstRelInfoViewVOTransform.toVOList(dtoList);
			
			Map<String, String> propTypCdMap = this.sysDictionaryApiService. // 查询客户类型
					searchGroupToMap(sysCd, orgCd, DictionaryConstant.SPL_ATTR_CD);
			BaseLogUtils.newWebLogger("客户类型数据字典").setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setKeys("propTypCdMap", JSON.toJSONString(propTypCdMap))
					.info();
			Map<String, String> relTypCdMap = this.sysDictionaryApiService. // 查询客户关系类型
					searchGroupToMap(sysCd, orgCd, DictionaryConstant.CST_REL_TYP_CD);
			BaseLogUtils.newWebLogger("客户关系类型数据字典").setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setKeys("relTypCdMap", JSON.toJSONString(relTypCdMap))
					.info();
			for (CstRelInfoViewVO cstRelInfoViewVO : voList) {
				if (null != cstRelInfoViewVO) {
					if (propTypCdMap.containsKey(cstRelInfoViewVO.getPropTypCd().toString())) {
						cstRelInfoViewVO.setPropTypCdStr(propTypCdMap.get(cstRelInfoViewVO.getPropTypCd().toString()));
					}
					if (relTypCdMap.containsKey(cstRelInfoViewVO.getRelTypCd())) {
						cstRelInfoViewVO.setRelTypCd(relTypCdMap.get(cstRelInfoViewVO.getRelTypCd()));
					}
				}
			}
			pager.setResultList(voList);
		}
		return pager;
	}


	@Override
	public List<CstRelInfoViewVO> searchList(CstRelInfoViewQC condition2) throws BaseException {
		// TODO Auto-generated method stub
		if (null == condition2) {
			throw new BaseException(BaseExceptionConstant.COMMON_0001, "查询条件不能为空!");
		}
//		return CstRelInfoViewVOTransform.toVOList(this.splVisitRecApiService.searchSplVisitRecList(condition2));
		return null;
	}
}
